package com.bite.redisDemo;

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Slf4j
public class RedisDemoString {

    public static void test1(Jedis jedis){
        System.out.println("mset 和 mget");
        jedis.mset("key1","111","key2","222","key3","333");
        List<String> result = jedis.mget("key1", "key100", "key2", "key3");
        System.out.println(result);
    }

    public static void test2(Jedis jedis){
        System.out.println("getrange 和 setrange");
        jedis.flushAll();
        jedis.set("key1","abcdefghijk");
        String result1 = jedis.getrange("key1", 0, -1);
        // 左闭右闭 [2,5]
        System.out.println(jedis.getrange("key1", 2, 5));
        System.out.println(result1);

        jedis.setrange("key1",2,"xyz");
        String result2 = jedis.get("key1");
        System.out.println(result2);
    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
//            test1(jedis);
            test2(jedis);
        }catch (Exception e){
            log.error("内部错误: "+e.getMessage());
        }
    }
}
